﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using AdvancedPoll.Models;
using Orchard.Data;

namespace AdvancedPoll.Services
{
    public class ExportService : IExportService
    {

        private readonly IRepository<PollRecord> _pollRepository;
        private readonly IRepository<PollChoiceRecord> _choiceRepository;
        private readonly IRepository<PollVoteRecord> _voteRepository;
        private readonly IRepository<PollResultRecord> _resultRepository;

        public ExportService(IRepository<PollRecord> pollRepository,
            IRepository<PollChoiceRecord> choiceRepository,
            IRepository<PollVoteRecord> voteRepository,
            IRepository<PollResultRecord> resultRepository)
        {
            _pollRepository = pollRepository;
            _choiceRepository = choiceRepository;
            _voteRepository = voteRepository;
            _resultRepository = resultRepository;
        }


        public void ExportToXml(int poll_id)
        {

            var poll = _pollRepository.Get(poll_id);
            var choice = _choiceRepository.Get(poll_id);
            var vote = _voteRepository.Get(poll_id);
            var result = _resultRepository.Get(poll_id);

            using (XmlWriter writer = XmlWriter.Create("Vote.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Vote");



                writer.WriteElementString("ID", poll.Id.ToString());
                writer.WriteElementString("Question", poll.Question.ToString());
                writer.WriteElementString("OpenDateUtc", poll.OpenDateUtc.ToString());
                writer.WriteElementString("CloseDateUtc", poll.CloseDateUtc.ToString());
                writer.WriteElementString("MaxVotes", poll.MaxVotes.ToString());
                writer.WriteElementString("Shown", poll.Shown.ToString());
                writer.WriteElementString("ContentItemRecord", poll.ContentItemRecord.ToString());




                writer.WriteEndElement();
                writer.WriteEndDocument();
            }



        }
    }
}
